package controller

import (
	"log"
	"testhttp/dao"
	"testhttp/model"
	"testhttp/service"
	"time"

	"github.com/kataras/iris/v12"
)

func ListOrder(ctx iris.Context) {
	var listparam model.HttpParamsOrder
	err := ctx.ReadJSON(&listparam)
	if err != nil && !iris.IsErrPath(err) {
		ctx.StatusCode(iris.StatusInternalServerError)
		ctx.WriteString(err.Error())
	} else {
		ctx.JSON(service.ListOrders(listparam))
	}
}

func Updatev(ctx iris.Context) {

}

func ViewOrder(ctx iris.Context) {

}

func AddOrder(ctx iris.Context) {
	var webrequest []model.Orders

	err := ctx.ReadJSON(&webrequest)
	if err != nil && !iris.IsErrPath(err) {
		ctx.StatusCode(iris.StatusInternalServerError)
		ctx.WriteString(err.Error())
		log.Print(err)
	} else {
		ctx.JSON(service.AddOrders(webrequest))
	}
}

func DelOrder(ctx iris.Context) {
	var webrequest, querydata []model.Orders
	// var z int
	now := time.Now()

	err := ctx.ReadJSON(&webrequest)
	if err != nil && !iris.IsErrPath(err) {
		ctx.StatusCode(iris.StatusInternalServerError)
		ctx.WriteString(err.Error())
		log.Print(err)
	} else {
		for x := 0; x < len(webrequest); x++ {
			webrequest[x].IsDeleted = 1
			webrequest[x].UpdateTime = now
			querydata = append(querydata, webrequest[x])
		}

		success, _ := dao.UpdateOrders(querydata)

		ctx.JSON(iris.Map{
			"code":    0,
			"msg":     "OK",
			"submit":  len(webrequest), //提交数据条数
			"success": success,         //成功插入数据
		})
	}
}
